﻿using EasyNet.Componets.Core.Http;
using EasyNet.Componets.Core.Inject;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace EasyNet.Componets.Core.Utils
{
    /// <summary>
    /// Http 辅助类
    /// </summary>
    public static class HttpUtil
    {
        #region Properties
        private static IHttpContext _httpContextInstance;

        public static IHttpContext HttpContextInstance
        {
            [DebuggerStepThrough]
            get
            {
                if (_httpContextInstance != null)
                {
                    return _httpContextInstance;
                }
                _httpContextInstance = IoC.Resolve<IHttpContext>();

                return _httpContextInstance;
            }
        }


        private static IHttpClient _httpClientInstance;

        public static IHttpClient HttpClientInstance
        {
            [DebuggerStepThrough]
            get
            {
                if (_httpClientInstance != null)
                {
                    return _httpClientInstance;
                }
                try
                {
                    _httpClientInstance = IoC.Resolve<IHttpClient>();
                }
                catch
                {
                    _httpClientInstance = new EasyNetHttpClient();
                }

                return _httpClientInstance;
            }
        }
        #endregion

        #region SetInstance
        /// <summary>
        /// 设置实例
        /// </summary>
        /// <param name="instance"></param>
        public static void SetHttpClientInstance(IHttpClient instance)
        {
            _httpClientInstance = instance;
        }

        /// <summary>
        /// 设置实例
        /// </summary>
        /// <param name="instance"></param>
        public static void SetHttpContextInstance(IHttpContext instance)
        {
            _httpContextInstance = instance;
        }
        #endregion

        #region Get
        public static string Get(string url)
        {
            return Get(url, null);
        }

        public static string Get(string url, Encoding encoding)
        {
            return Get(url, encoding, null);
        }

        public static string Get(string url, Encoding encoding, Dictionary<string, string> headerList)
        {
            var cookieContainer = new CookieContainer();
            return Get(url, encoding, null, ref cookieContainer);
        }

        public static string Get(string url, Encoding encoding, Dictionary<string, string> headerList, ref CookieContainer cookieContainer)
        {
            return HttpClientInstance.Get(url, encoding, headerList, ref cookieContainer);
        }

        #endregion

        #region Get Async
        public static async Task<string> GetAsync(string url)
        {
            return await GetAsync(url, null);
        }


        public static async Task<string> GetAsync(string url, Encoding encoding)
        {
            return await GetAsync(url, encoding, null);
        }

        public static async Task<string> GetAsync(string url, Encoding encoding, Dictionary<string, string> headerList)
        {
            var cookieContainer = new CookieContainer();
            return await GetAsync(url, encoding, headerList, cookieContainer);
        }

        public static async Task<string> GetAsync(string url, Encoding encoding, Dictionary<string, string> headerList, CookieContainer cookieContainer)
        {
            return await HttpClientInstance.GetAsync(url, encoding, headerList, cookieContainer);
        }

        #endregion

        #region Post
        public static string Post(string url, string body, string contentType)
        {
            return Post(url, body, contentType, null);
        }

        public static string Post(string url, string body, string contentType, Encoding encoding)
        {
            return Post(url, body, contentType, encoding, null);
        }

        public static string Post(string url, string body, string contentType, Encoding encoding, Dictionary<string, string> headerList)
        {
            var cookieContainer = new CookieContainer();
            return Post(url, body, contentType, encoding, headerList, ref cookieContainer);
        }

        public static string Post(string url, string body, string contentType, Encoding encoding, Dictionary<string, string> headerList, ref CookieContainer cookieContainer)
        {
            return HttpClientInstance.Post(url, body, contentType, encoding, headerList, ref cookieContainer);
        }

        #endregion


        #region Update
        public static string Update(string url, string body, string contentType)
        {
            return Update(url, body, contentType, null);
        }

        public static string Update(string url, string body, string contentType, Encoding encoding)
        {
            return Update(url, body, contentType, encoding, null);
        }

        public static string Update(string url, string body, string contentType, Encoding encoding, Dictionary<string, string> headerList)
        {
            var cookieContainer = new CookieContainer();
            return Update(url, body, contentType, encoding, headerList, ref cookieContainer);
        }

        public static string Update(string url, string body, string contentType, Encoding encoding, Dictionary<string, string> headerList, ref CookieContainer cookieContainer)
        {
            return HttpClientInstance.Update(url, body, contentType, encoding, headerList, ref cookieContainer);
        }

        #endregion

        #region Post Async
        public static async Task<string> PostAsync(string url, string body, string contentType)
        {
            return await PostAsync(url, body, contentType, null);
        }

        public static async Task<string> PostAsync(string url, string body, string contentType, Encoding encoding)
        {
            return await PostAsync(url, body, contentType, encoding, null);
        }

        public static async Task<string> PostAsync(string url, string body, string contentType, Encoding encoding, Dictionary<string, string> headerList)
        {
            var cookieContainer = new CookieContainer();
            return await PostAsync(url, body, contentType, encoding, headerList, cookieContainer);
        }

        public static async Task<string> PostAsync(string url, string body, string contentType, Encoding encoding, Dictionary<string, string> headerList, CookieContainer cookieContainer)
        {
            return await HttpClientInstance.PostAsync(url, body, contentType, encoding, headerList, cookieContainer);
        }

        #endregion

        #region Delete

        public static string Delete(string url)
        {
            var cookieContainer = new CookieContainer();
            return HttpClientInstance.Delete(url, null, null, ref cookieContainer);
        }
        #endregion

        #region HttpContext

        public static string GetRequestContent(object request)
        {
            return HttpContextInstance.GetRequestContent(request);

        }

        public static RequestInfo GetRequestInfo(object request)
        {
            return HttpContextInstance.GetRequestInfo(request);
        }

        public static string GetResponseContent(object response)
        {
            return HttpContextInstance.GetResponseContent(response);
        }

        public static ResponseInfo GetResponseInfo(object response)
        {
            return HttpContextInstance.GetResponseInfo(response);
        }

        public static string GetClientIp(object request)
        {
            return HttpContextInstance.GetClientIp(request);
        }

        public static void SetRequestId(object request, string requestId)
        {
            HttpContextInstance.SetRequestId(request, requestId);
        }

        public static string GetRequestId(object request)
        {
            return HttpContextInstance.GetRequestId(request);
        }

        public static RequestAuthenticationHeaderValue GetHeaderAuthorization(object request)
        {
            return HttpContextInstance.GetHeaderAuthorization(request);
        }
        #endregion
    }
}
